package com.bsoft.gol.hcs.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.io.*;

/**
 * @Author: lanyu
 * @Description:
 * @Date: create in 2022/7/15 11:30
 */
@Component
public class JSONDateUtils {
    private final static Logger log = LoggerFactory.getLogger(JSONDateUtils.class);

    public static String gdpathPrx = "";

    //正式环境请注释如下代码块，不然可能会抛异常
    static {
        if (IPUtil.cheakIPInLocalIPList("10.42.123.59") || IPUtil.cheakIPInLocalIPList("10.8.3.178")
                || IPUtil.cheakIPInLocalIPList("10.8.3.29")) {
            //质控测试服务器
            gdpathPrx = "/usr/local/tomcat/webapps/common-clinical/WEB-INF/classes/txt/";
        } else if (IPUtil.cheakIPInLocalIPList("10.0.38.78") || IPUtil.cheakIPInLocalIPList("10.0.38.49")
                || IPUtil.cheakIPInLocalIPList("192.168.2.101") || IPUtil.cheakIPInLocalIPList("10.0.38.31")
                || IPUtil.cheakIPInLocalIPList("10.1.2.145") || IPUtil.cheakIPInLocalIPList("10.1.2.39")
                || IPUtil.cheakIPInLocalIPList("192.168.100.2") || IPUtil.cheakIPInLocalIPList("192.168.100.32")
                || IPUtil.cheakIPInLocalIPList("192.168.34.212") || IPUtil.cheakIPInLocalIPList("192.168.105.213")
                || IPUtil.cheakIPInLocalIPList("192.168.43.122")) {
            //开发环境在后面加|| IPUtil.cheakIPInLocalIPList("您的IP")
            gdpathPrx = JSONDateUtils.class.getResource("/txt/").getPath();
        }else if (IPUtil.cheakIPInLocalIPList("10.8.3.125")) {
            //互联互通服务器
            gdpathPrx = "D:/HLW/jsondata/";
        }else if (IPUtil.cheakIPInLocalIPList("10.8.3.57")) {
            //开发环境服务器
            gdpathPrx = "D:/dev/data/";
        }else {
//            gdpathPrx = ClassLoader.getSystemResource("")+"/txt/";
            gdpathPrx = JSONDateUtils.class.getResource("/")+ "/txt/";
            log.info("gdpathPrx:"+gdpathPrx);
            String os = System.getProperty("os.name");
            if(os.toLowerCase().startsWith("win")){
                gdpathPrx = gdpathPrx.replace("file:/","").replace("file:\\","");
            }else{
                gdpathPrx = gdpathPrx.replace("file:","");
            }



            log.info(gdpathPrx);
        }
    }

    public static void main(String[] args) {
        String jsonContext = JSONDateUtils.readFile("crucialIndex");
        System.out.println(jsonContext);
    }

    public static String readFile(String fileName) {
        log.info("fileName:{}", fileName);
        String laststr = "";
        try (InputStream resourceAsStream =
                     JSONDateUtils.class.getClassLoader().getResourceAsStream("txt/" + fileName + ".txt");
             InputStreamReader inputStreamReader = new InputStreamReader(resourceAsStream, "UTF-8");
             BufferedReader reader = new BufferedReader(inputStreamReader)) {
            String tempString;
            while ((tempString = reader.readLine()) != null) {
                laststr += tempString;
            }
        } catch (IOException e) {
            log.error("读取" + fileName + ".txt文件失败", e);
        }
        return laststr;
    }

    /**
     * 读取体检问卷调查文件
     * @param @param fileName
     * @param @return    入参
     * @return String    返回类型
     * @author hujt   @date 2017年11月3日 下午1:00:32
     */
    public static String readQuestionnaireFile(String fileName) {
        String file=fileName+".txt";
        BufferedReader reader = null;
        String laststr = "";
        try {
            FileInputStream fileInputStream = new FileInputStream(file);
            InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, "UTF-8");
            reader = new BufferedReader(inputStreamReader);
            String tempString = null;
            while ((tempString = reader.readLine()) != null) {
                laststr += tempString;
            }
            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return laststr;
    }
}
